package com.eternal.base.data.source;

import java.util.Collections;
import java.util.List;

public class MinNumberUtil {
    /**
     * 获取默认最小下标
     *
     * @param list
     * @return
     */
    static int minNumber(List<Integer> list) {
        if (list.isEmpty()) {
            return 1;
        } else if (list.size() == 1) {
            if (list.get(0) == 1) {
                return 2;
            } else {
                return 1;
            }
        }
        int min = 0, i, j;
        for (i = 1; i < list.size(); ++i) {
            if (list.get(min) > list.get(i)) {
                min = i;
            }
        }
        if (list.get(min) != 1) {
            return 1;
        }
        Collections.swap(list, min, 0);
        for (i = 1; i < list.size(); ++i) {
            min = i;
            for (j = i + 1; j < list.size(); ++j) {
                if (list.get(min) > list.get(j)) {
                    min = j;
                }
            }
            Collections.swap(list, i, min);
            if (list.get(i) - list.get(i - 1) > 1) {
                return list.get(i - 1) + 1;
            }
        }
        return list.size() + 1;
    }

    static int minNumberZero(List<Integer> list) {
        if (list.isEmpty()) {
            return 0;
        } else if (list.size() == 1) {
            if (list.get(0) == 0) {
                return 1;
            } else {
                return 0;
            }
        }
        int min = 0, i, j;
        for (i = 1; i < list.size(); ++i) {
            if (list.get(min) > list.get(i)) {
                min = i;
            }
        }
        if (list.get(min) != 0) {
            return 0;
        }
        Collections.swap(list, min, 0);
        for (i = 1; i < list.size(); ++i) {
            min = i;
            for (j = i + 1; j < list.size(); ++j) {
                if (list.get(min) > list.get(j)) {
                    min = j;
                }
            }
            Collections.swap(list, i, min);
            if (list.get(i) - list.get(i - 1) > 1) {
                return list.get(i - 1) + 1;
            }
        }
        return list.size();
    }

    static void getNumberZero(List<Integer> numbers, String prefix, String name) {
        if (name.startsWith(prefix)) {
            if (name.length() == prefix.length()) {
                numbers.add(0);
            } else {
                try {
                    numbers.add(Integer.valueOf(name.substring(prefix.length() + 1)));
                } catch (NumberFormatException e) {
                }
            }
        }
    }

    static void getNumber(List<Integer> numbers, String prefix, String name) {
        if (name.startsWith(prefix)) {
            if (name.length() == prefix.length()) {
                numbers.add(0);
            } else {
                try {
                    numbers.add(Integer.valueOf(name.substring(prefix.length() + 1)));
                } catch (NumberFormatException e) {
                }
            }
        }
    }
}
